package com.orange.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MusicPreferences {
	private static SharedPreferences	sSp;
	private static Editor				sEditor;
	
	public static final String MUSIC_OLD_STATUS = "music_old_status";
	public static final String MUSIC_OLD_STATUS_PLAYING = "playing";
	public static final String MUSIC_OLD_STATUS_PAUSE = "pause";
	public static final String MUSIC_OLD_STATUS_STOP = "stop";
	public static final String IS_MUSIC_SLAT_OPENED = "is_music_slat_opened";

	public static void init(Context context) {
		sSp = context.getSharedPreferences(Constants.PREFERENCES_MUSIC_NAME,
				Context.MODE_WORLD_WRITEABLE);
		sEditor = sSp.edit();

		setLockScreenLoad(context, 0);
	}

	/**
	 * Identify lockscreen status
	 * 
	 * @param context
	 * @param isLoad
	 *            1 indicates enter lockscreen, 0 indicates quit lockscreen.
	 */
	public static void setLockScreenLoad(Context context, int isLoad) {
		if (null != sSp) {
			sEditor.putInt(Constants.KEY_LOCKSCREEN_LOAD, isLoad);
			sEditor.commit();
		}
	}

	/**
	 * Whether lockscreen has been load.
	 * 
	 * @param context
	 * @return 0 represent not show. Other values represent show.
	 */
	public static int getLockScreenStatus(Context context) {
		if (null != sSp) {
			return sSp.getInt(Constants.KEY_LOCKSCREEN_LOAD, 0);
		}

		return -1;
	}
	/**
	 * add by gaoge 2011-12-23,to save music status
	 * @param context
	 * @param oldStatus
	 */
	public static void setMusicPlayOldStatus(Context context, String oldStatus) {
		if (null != sSp) {
			sEditor.putString(MUSIC_OLD_STATUS, oldStatus);
			sEditor.commit();
		}
	}

	/**
	 * add by gaoge 2011-12-23,to get music status
	 * @param context
	 * @param oldStatus
	 */
	public static String getMusicPlayOldStatus(Context context) {
		if (null != sSp) {
			return sSp.getString(MUSIC_OLD_STATUS, MUSIC_OLD_STATUS_PAUSE);
		}

		return MUSIC_OLD_STATUS_PAUSE;
	}
	
	/**
	 * add by gaoge 2011-12-25,to save music status
	 * @param context
	 * @param oldStatus
	 */
	public static void setMusicSlatOpened(Context context, boolean isOpened) {
		if (null != sSp) {
			sEditor.putBoolean(IS_MUSIC_SLAT_OPENED, isOpened);
			sEditor.commit();
		}
	}

	/**
	 * add by gaoge 2011-12-25,to get music status
	 * @param context
	 * @param oldStatus
	 */
	public static boolean getMusicSlatOpened(Context context) {
		boolean isMusicSlatOpened = false;
		if (null != sSp) {
			return sSp.getBoolean(IS_MUSIC_SLAT_OPENED, false);
		}

		return isMusicSlatOpened;
	}

	// /**
	// * Indicates music track change(Or album change) from the user click
	// button, instead of from system.
	// * @param context
	// * @param flag "true" indicates music track change from user operation.
	// "false" indicates automatically from system
	// */
	// public static void setMusicTrackChangeByUser(Context context, boolean
	// fromUser) {
	// if (null != mSp) {
	// mEditor.putBoolean(Constants.KEY_MUSIC_USER_ACTION, fromUser);
	// mEditor.commit();
	// }
	// }
	//
	// /**
	// * Indicates music track change(Or album change) from the user click
	// button, instead of from system.
	// * @return "true" indicates music track change from user operation.
	// "false" indicates automatically from system
	// * @param context
	// */
	// public static boolean isMusicTrackChangeByUser(Context context) {
	// if (null != mSp) {
	// return mSp.getBoolean(Constants.KEY_MUSIC_USER_ACTION, false);
	// }
	//
	// return false;
	// }

	/**
	 * 
	 * @param context
	 * @param value
	 *            true indicates music action from other devices(e.g bluetooth). <br/>
	 *            false indicates from system
	 */
	public static void setBroadcastFromBT(Context context, boolean value) {
		if (null != sSp) {
			sEditor.putBoolean(Constants.KEY_MUSIC_ACTION_FROM_BT, value);
		}
	}

	/**
	 * @param context
	 * @return true indicates music action from other devices(e.g bluetooth). <br/>
	 *         false indicates from system
	 */
	public static boolean isBroadcastFromBT(Context context) {
		if (null != sSp) {
			return sSp.getBoolean(Constants.KEY_MUSIC_ACTION_FROM_BT, false);
		}

		return false;
	}
}
